﻿using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Rapor
{
    public partial class KumasIrsaliye : DevReport
    {     
        public KumasIrsaliye()
        {
            InitializeComponent();
        }

        public KumasIrsaliye(string IrsaliyeNo, DateTime SevkTarihi, string Aciklama, string CikisEmriId,  int UserId,int Tip,int Cari,string CikanPartiIds,string TeslimEden,string SpName)
            : this()
        {
            tcTeslimEden.Text = TeslimEden;
            DataSet dsData = Session.DataManager.ExecuteDataSet(SpName, IrsaliyeNo, SevkTarihi, Aciklama, CikisEmriId, UserId, Tip, Cari, CikanPartiIds);
            FillDataSet(dsData);
        }

        public KumasIrsaliye(string IrsaliyeNo, DateTime SevkTarihi, string Aciklama, int IrsaliyeId, int UserId, int Tip, int Cari, string CikanPartiIds, string TeslimEden)
            : this()
        {
            tcTeslimEden.Text = TeslimEden;
            DataSet dsData = ExecuteDataSet("GetKumasIrsaliyeByIrsaliyeId", IrsaliyeNo, SevkTarihi, Aciklama, IrsaliyeId, UserId, Tip, Cari, CikanPartiIds);
            FillDataSet(dsData);
        }

        public KumasIrsaliye(string IrsaliyeNo, int WhoAmI, string SpName):this()
        {
            DataSet dsData = ExecuteDataSet(SpName, IrsaliyeNo, WhoAmI); 
            FillDataSet(dsData);
        }

        public KumasIrsaliye(string IrsaliyeNo, string RollIds, int WhoAmI, string SpName):this()
        {
            DataSet dsData = ExecuteDataSet(SpName, IrsaliyeNo, RollIds, WhoAmI);
            FillDataSet(dsData);
        }
        private void FillDataSet(DataSet dsData)
        {
            myDataKumas.Merge(dsData);
            myDataKumas.KumasIrsaliye.Merge(myDataKumas.Tables["Table"]);
            myDataKumas.KumasIrsaliyeLines.Merge(myDataKumas.Tables["Table1"]);

            KumasIrsaliyeLines KumasIrsaliyeLines1 = (KumasIrsaliyeLines)xrSubreport1.ReportSource;
            KumasIrsaliyeLines1.fillTables(myDataKumas.KumasIrsaliyeLines);
        }

        void xrTableCell32_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            int factor;

            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            if (this.ReportUnit == ReportUnit.HundredthsOfAnInch)
            {
                gr.PageUnit = GraphicsUnit.Inch;
                factor = 100;
            }
            else
            {
                gr.PageUnit = GraphicsUnit.Millimeter;
                factor = 10;
            }

            SizeF size = gr.MeasureString(((XRLabel)sender).Text, ((XRLabel)sender).Font);
            //((XRLabel)sender).Width = Convert.ToInt32(size.Width * factor);


            if (((XRLabel)sender).Width < size.Width * factor)
                ((XRLabel)sender).Font = new System.Drawing.Font("Microsoft Sans Serif", ((XRLabel)sender).Font.Size * ((XRLabel)sender).Width / size.Width / factor * 1.025F);

            gr.Dispose();
        }

        private void xrSubreport1_BeforePrint_1(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ((KumasIrsaliyeLines)((XRSubreport)sender).ReportSource).TalepID.Value =
       Convert.ToInt32(GetCurrentColumnValue("KumasCikisTalebiId"));
        }

        private void xrTable12_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            XtraReport rep = (XtraReport)((XRTable)sender).Report;
            int PageCount = rep.PrintingSystem.Document.PageCount+1;

            decimal ToplamMetre = 0;
            int ToplamAdet = 0;
            string selectString = "RN > " + ((PageCount - 1) * 31).ToString() + " and RN <= " + ((PageCount) * 31).ToString();
            foreach (DataRow dr in myDataKumas.KumasIrsaliyeLines.Select(selectString))
            {
                decimal val;
                decimal.TryParse(dr["Metre"].ToString(), out val);
                ToplamMetre += val;

                ToplamAdet += dr["Adet"].ToInt();
            }

            tcToplamMetre.Text = decimal.Round(ToplamMetre, 1).ToString() + " mt.";
            tcToplamAdet.Text = ToplamAdet.ToString() + " ad.";
        }

    }
}
